package com.liuguang.realgateway.handler;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.liuguang.common.pojo.CommonResult;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @ClassName: MyBlockRequestHandler
 * @Description：限流异常处理类，在MySentinelConfig 配置类的初始化方法中注入到 GatewayRulesManager 中
 * @Author: wanghr
 * Date: 6:59 PM 2020/11/2
 * Version: 1.0
 */
public class MyBlockRequestHandler implements BlockRequestHandler {
    @Override
    public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
        System.out.println("进入MyBlockRequestHandler");
        //没必要加类型的判断，在SentinelGatewayBlockExceptionHandler 已经做过判断才会进入到这
//        if (BlockException.isBlockException(throwable)) {
        BlockException e = (BlockException) throwable;
        return ServerResponse.ok().body(BodyInserters.fromValue(CommonResult.fail("服务限流。！！！！！！")));
//        }
    }
}
